home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 7
/
Apprentice-Release7.iso
/
Source Code
/
Pascal
/
Applications
/
TCPExample
/
PNL Libraries
/
MyStrings.p
< prev
next >
Wrap
Text File
|
1997-07-16
|
15KB
|
632 lines
unit MyStrings;
interface
uses
Types, MyTypes;
procedure LeftP (var s: Str255; len: integer);
function LeftF (var s: Str255; len: integer): Str255;
procedure LeftAssignP (var s: Str255; len: integer; var rhs: Str255);
function LeftAssign (var s: Str255; len: integer; var rhs: Str255): Str255;
procedure RightP (var s: Str255; len: integer);
function RightF (var s: Str255; len: integer): Str255;
procedure RightAssignP (var s: Str255; len: integer; var rhs: Str255);
function RightAssign (var s: Str255; len: integer; var rhs: Str255): Str255;
procedure MidP (var s: Str255; p, len: integer);
function Mid (var s: Str255; p, len: integer): Str255;
procedure MidAssignP (var s: Str255; p, len: integer; const rhs: Str255);
function MidAssign (const s: Str255; p, len: integer; const rhs: Str255): Str255;
procedure HandleToString (hhhh: univ Handle; var s: Str255);
function HandleToStr (hhhh: univ Handle): Str255;
procedure StringToHandle (const s: Str255; hhhh: univ Handle);
function Trim (s: string): string;
function LowerCase( ch: char ): char;
function UpCaseChar (ch: char): char;
{$IFC not GENERATINGPOWERPC}
inline
$301F, $0C00, $0061, $6500, $000E, $0C00, $007B, $6400, $0006, $0400, $0020, $3E80;
{$ENDC}
function IsDigit(ch:char):boolean;
{$IFC not GENERATINGPOWERPC}
inline
$321F,$0C41,$0030,$5CC0,$6D08,$0C41,$0039,$6F02,$5FC0,$4400,$1E80;
{$ENDC}
function IsLower(ch:char):boolean;
{$IFC not GENERATINGPOWERPC}
inline
$321F,$0C41,$0061,$5CC0,$6D08,$0C41,$007A,$6F02,$5FC0,$4400,$1E80;
{$ENDC}
function IsUpper(ch:char):boolean;
{$IFC not GENERATINGPOWERPC}
inline
$321F,$0C41,$0041,$5CC0,$6D08,$0C41,$005A,$6F02,$5FC0,$4400,$1E80;
{$ENDC}
function IsAlpha(ch:char):boolean;
{$IFC not GENERATINGPOWERPC}
inline
$321F,$0C41,$0041,$5CC0,$6D16,$0C41,$005A,$6F10,$0C41,$0061,$5CC0,$6D08,$0C41,$007A,$6F02,$5FC0,$4400,$1E80;
{$ENDC}
procedure UpCaseString (var s: string);
function UpCaseStr (s: string): string;
procedure LowerCaseString (var s: string);
function LowerCaseStr (s: string): string;
function NoCaseEquals( s1, s2: string ): boolean;
function NoCasePos( s1, s2: string ): integer;
procedure SPrintS5 (var dst: Str255; const src, s1, s2, s3, s4, s5: Str255);
procedure SPrintS3 (var dst: Str255; const src, s1, s2, s3: Str255);
function PosRight (sub: char; const s: Str255): integer;
function PosRightStr (const sub, s: Str255): integer;
function Contains( sub: char; const s: Str255 ): boolean;
function ContainsStr( const sub, s: Str255 ): boolean;
procedure SplitBy (s: Str255; sub: char; var left, right: Str255);
procedure SplitRightBy (s: Str255; sub: char; var left, right: Str255);
procedure SplitByStr (s: Str255; const sub: Str255; var left, right: Str255);
procedure SplitRightByStr (s: Str255; const sub: Str255; var left, right: Str255);
function SplitAt (s: Str255; sub: char; var s1, s2: Str255): boolean;
function SplitRightAt(s: Str255; sub: char; var s1, s2: Str255): boolean;
function SplitAtStr (s: Str255; const sub: Str255; var s1, s2: Str255): boolean;
function SplitRightAtStr (s: Str255; const sub: Str255; var s1, s2: Str255): boolean;
{ function Pos (sub, str: string): integer;}
function TPcopy (source: string; start, count: integer): string;
function Match (pattern, name: Str255): boolean;
procedure LimitStringLength (var s: string; len: integer; delimiter: char);
function StringToOSType (const s: Str255): OSType;
function OSTypeToString (t: OSType): Str255;
function FindCharacter(p:Ptr; len:longint; ch:Char; var pos:longint):boolean;
implementation
uses
Memory, OSUtils, TextUtils, MyMathUtils, MyLowLevel, MyMemory;
function FindCharacter(p:Ptr; len:longint; ch:Char; var pos:longint):boolean;
begin
pos:=0;
while (pos<len) & (AddPtrLong(p,pos)^<>ord(ch)) do begin
pos:=pos+1;
end;
FindCharacter:= pos<len;
end;
procedure LeftP (var s: Str255; len: integer);
begin
s := TPcopy(s, 1, len);
end;
function LeftF (var s: Str255; len: integer): Str255;
begin
LeftF := TPcopy(s, 1, len);
end;
procedure LeftAssignP (var s: Str255; len: integer; var rhs: Str255);
begin
s := concat(rhs, TPcopy(s, len + 1, 255));
end;
function LeftAssign (var s: Str255; len: integer; var rhs: Str255): Str255;
begin
LeftAssign := concat(rhs, TPcopy(s, len + 1, 255));
end;
procedure RightP (var s: Str255; len: integer);
var
p: integer;
begin
p := Length(s) - len;
if p < 1 then begin
p := 1;
end;
s := TPcopy(s, p, 255);
end;
function RightF (var s: Str255; len: integer): Str255;
var
p: integer;
begin
p := Length(s) - len;
if p < 1 then begin
p := 1;
end;
RightF := TPcopy(s, p, 255);
end;
procedure RightAssignP (var s: Str255; len: integer; var rhs: Str255);
begin
s := concat(TPcopy(s, 1, Length(s) - len), rhs);
end;
function RightAssign (var s: Str255; len: integer; var rhs: Str255): Str255;
begin
RightAssign := concat(TPcopy(s, 1, Length(s) - len), rhs);
end;
procedure MidP (var s: Str255; p, len: integer);
begin
s := TPcopy(s, p, len);
end;
function Mid (var s: Str255; p, len: integer): Str255;
begin
Mid := TPcopy(s, p, len);
end;
procedure MidAssignP (var s: Str255; p, len: integer; const rhs: Str255);
begin
s := concat(TPcopy(s, 1, p - 1), rhs, TPcopy(s, p + len, 255));
end;
function MidAssign (const s: Str255; p, len: integer; const rhs: Str255): Str255;
begin
MidAssign := concat(TPcopy(s, 1, p - 1), rhs, TPcopy(s, p + len, 255));
end;
{$PUSH}
{$R-}
procedure HandleToString (hhhh: univ Handle; var s: Str255);
var
len: longint;
begin
len := Min(255, MGetHandleSize(hhhh));
s[0] := chr(len);
BlockMoveData(hhhh^, @s[1], len);
end;
{$POP}
function HandleToStr (hhhh: univ Handle): Str255;
var
s: Str255;
begin
HandleToString(hhhh, s);
HandleToStr := s;
end;
procedure StringToHandle (const s: Str255; hhhh: univ Handle);
begin
SetHandleSize(hhhh, length(s));
if (MemError = noErr) & (length(s) > 0) then begin
BlockMoveData(@s[1], hhhh^, length(s));
end;
end;
function Trim (s: string): string;
begin
while (length(s) > 0) and (s[1] in [spc, tab, cr, lf]) do begin
Delete(s, 1, 1);
end;
while (length(s) > 0) and (s[length(s)] in [spc, tab, cr, lf]) do begin
Delete(s, length(s), 1);
end;
Trim := s;
end;
function LowerCase( ch: char ): char;
begin
if ('A' <= ch) & (ch <= 'Z') then begin
ch := chr(ord(ch) + $20);
end;
LowerCase := ch;
end;
{$IFC GENERATINGPOWERPC}
function UpCaseChar (ch: char): char;
begin
if ('a' <= ch) & (ch <= 'z') then begin
ch := chr(ord(ch) - $20);
end;
UpCaseChar := ch;
end;
function IsDigit(ch:char):boolean;
begin
IsDigit:=('0'<=ch) & (ch<='9');
end;
function IsLower(ch:char):boolean;
begin
IsLower:=('a'<=ch) & (ch<='z');
end;
function IsUpper(ch:char):boolean;
begin
IsUpper:=('A'<=ch) & (ch<='Z');
end;
function IsAlpha(ch:char):boolean;
begin
IsAlpha:=(('a'<=ch) & (ch<='z')) | (('A'<=ch) & (ch<='Z'));
end;
{$ENDC}
function NoCaseEquals( s1, s2: string ): boolean;
begin
LowerCaseString( s1 );
LowerCaseString( s2 );
NoCaseEquals := s1 = s2;
end;
function NoCasePos( s1, s2: string ): integer;
begin
LowerCaseString( s1 );
LowerCaseString( s2 );
NoCasePos := Pos( s1, s2 );
end;
procedure LowerCaseString (var s: string);
var
i: integer;
begin
for i := 1 to length(s) do begin
s[i] := LowerCase(s[i]);
end;
end;
function LowerCaseStr (s: string): string;
var
i: integer;
begin
for i := 1 to length(s) do begin
s[i] := LowerCase(s[i]);
end;
LowerCaseStr := s;
end;
procedure UpCaseString (var s: string);
var
i: integer;
begin
for i := 1 to length(s) do begin
s[i] := UpCaseChar(s[i]);
end;
end;
function UpCaseStr (s: string): string;
var
i: integer;
begin
for i := 1 to length(s) do begin
s[i] := UpCaseChar(s[i]);
end;
UpCaseStr := s;
end;
function TPcopy (source: string; start, count: integer): string;
begin
if (start < 1) then begin
count := count - (1 - start);
start := 1;
end;
if start + count > length(source) then begin
count := length(source) - start + 1;
end;
if count < 0 then begin
count := 0;
end;
source[0] := chr(count);
BlockMoveData(@source[start], @source[1], count);
TPcopy := source;
end;
{
function Pos (sub, str: string): integer;
var
i, j, ret: integer;
begin
i := 1;
ret := 1;
if length(sub) > 0 then begin
ret := 0;
while (i <= length(str) - length(sub) + 1) do begin
if str[i] = sub[1] then begin
j:=2;
while j<=length(sub) do begin
if str[i+j-1]<>sub[j] then begin
leave;
end;
j:=j+1;
end;
if j>length(sub) then begin
ret:=i;
leave;
end;
end;
i := i + 1;
end;
end;
Pos := ret;
end;
} procedure DoSub (var dst: Str255; n: integer; const s: Str255);
var
p: integer;
begin
p := Pos(concat('^', chr(n + 48)), dst);
if p > 0 then begin
Delete(dst, p, 2);
Insert(s, dst, p);
end;
end;
procedure SPrintS5 (var dst: Str255; const src, s1, s2, s3, s4, s5: Str255);
var
temp: Str255;
begin
temp := src;
DoSub(temp, 5, s5);
DoSub(temp, 4, s4);
DoSub(temp, 3, s3);
DoSub(temp, 2, s2);
DoSub(temp, 1, s1);
dst := temp;
end;
procedure SPrintS3 (var dst: Str255; const src, s1, s2, s3: Str255);
var
temp: Str255;
begin
temp := src;
DoSub(temp, 3, s3);
DoSub(temp, 2, s2);
DoSub(temp, 1, s1);
dst := temp;
end;
function PosRight (sub: char; const s: Str255): integer;
var
p: integer;
begin
p := length(s);
while p > 0 do begin
if s[p] = sub then begin
leave;
end;
Dec(p);
end;
PosRight := p;
end;
function PosRightStr (const sub, s: Str255): integer;
var
p, q: integer;
begin
p := Pos(sub, s);
if p > 0 then begin
q := length(s) - length(sub) + 1;
while q > p do begin
if TPcopy(s, q, length(sub)) = sub then begin
p := q;
end else begin
q := q - 1;
end;
end;
end;
PosRightStr := p;
end;
function Contains( sub: char; const s: Str255 ): boolean;
begin
Contains := Pos( sub, s ) > 0;
end;
function ContainsStr( const sub, s: Str255 ): boolean;
begin
ContainsStr := Pos( sub, s ) > 0;
end;
procedure SplitBy (s: Str255; sub: char; var left, right: Str255);
var
p: integer;
begin
p := Pos(sub, s);
if p <= 0 then begin
left := s;
right := '';
end else begin
left := TPcopy(s, 1, p - 1);
right := TPcopy(s, p + 1, 255);
end;
end;
procedure SplitRightBy (s: Str255; sub: char; var left, right: Str255);
var
p: integer;
begin
p := PosRight(sub, s);
if p <= 0 then begin
left := '';
right := s;
end else begin
left := TPcopy(s, 1, p - 1);
right := TPcopy(s, p + 1, 255);
end;
end;
procedure SplitByStr (s: Str255; const sub: Str255; var left, right: Str255);
var
p: integer;
begin
p := Pos(sub, s);
if p <= 0 then begin
left := s;
right := '';
end else begin
left := TPcopy(s, 1, p - 1);
right := TPcopy(s, p + 1, 255);
end;
end;
procedure SplitRightByStr (s: Str255; const sub: Str255; var left, right: Str255);
var
p: integer;
begin
p := PosRightStr(sub, s);
if p <= 0 then begin
left := '';
right := s;
end else begin
left := TPcopy(s, 1, p - 1);
right := TPcopy(s, p + 1, 255);
end;
end;
function SplitAt (s: Str255; sub: char; var s1, s2: Str255): boolean;
var
p: integer;
begin
p := Pos(sub, s);
if p > 0 then begin
s1 := TPcopy(s, 1, p - 1);
s2 := TPcopy(s, p + 1, 255);
end;
SplitAt := p > 0;
end;
function SplitRightAt(s: Str255; sub: char; var s1, s2: Str255): boolean;
var
p: integer;
begin
p := PosRight(sub, s);
if p > 0 then begin
s1 := TPcopy(s, 1, p - 1);
s2 := TPcopy(s, p + 1, 255);
end;
SplitRightAt := p > 0;
end;
function SplitAtStr (s: Str255; const sub: Str255; var s1, s2: Str255): boolean;
var
p: integer;
begin
p := Pos(sub, s);
if p > 0 then begin
s1 := TPcopy(s, 1, p - 1);
s2 := TPcopy(s, p + length(sub), 255);
end;
SplitAtStr := p > 0;
end;
function SplitRightAtStr (s: Str255; const sub: Str255; var s1, s2: Str255): boolean;
var
p: integer;
begin
p := PosRightStr(sub, s);
if p > 0 then begin
s1 := TPcopy(s, 1, p - 1);
s2 := TPcopy(s, p + length(sub), 255);
end;
SplitRightAtStr := p > 0;
end;
function Match (pattern, name: Str255): boolean;
function M (p, n: integer): boolean;
var
state: (searching, failed, success);
begin
state := searching;
while state = searching do begin
case ord(p <= length(pattern)) * 2 + ord(n <= length(name)) of
0: begin
state := success;
end;
1: begin
state := failed;
end;
2: begin
state := success;
while p <= length(pattern) do begin
if pattern[p] <> '*' then begin
state := failed;
leave;
end;
p := p + 1;
end;
end;
3: begin
case pattern[p] of
'?': begin
p := p + 1;
n := n + 1;
end;
'*': begin
p := p + 1;
if p > length(pattern) then begin { short circuit the * at the end case }
state := success;
end else begin
state := failed;
while n <= length(name) do begin
if M(p, n) then begin
state := success;
leave;
end;
n := n + 1;
end;
end;
end;
otherwise begin
if name[n] <> pattern[p] then begin
state := failed;
end;
n := n + 1;
p := p + 1;
end;
end;
end;
end;
end;
M := state = success;
end;
begin
UpperString(pattern, false);
UpperString(name, false);
Match := M(1, 1);
end;
procedure LimitStringLength (var s: string; len: integer; delimiter: char);
var
p: integer;
begin
if length(s) > len then begin
p := Pos(delimiter, s);
if p <= 0 then begin
p := length(s) div 2 + 1;
s[p] := delimiter;
end;
while length(s) > len do begin
if p > len div 2 + 1 then begin
Delete(s, p - 1, 1);
p := p - 1;
end else begin
Delete(s, p + 1, 1);
end;
end;
end;
end;
function StringToOSType (const s: Str255): OSType;
var
t: OSType;
begin
if length(s) >= 4 then begin
BlockMoveData(@s[1], @t, 4);
end else begin
t := OSType(0);
BlockMoveData(@s[1], @t, length(s));
end;
StringToOSType := t;
end;
function OSTypeToString (t: OSType): Str255;
var
s:Str255;
begin
s:=concat(nul,nul,nul,nul);
BlockMoveData(@t,@s[1],4);
OSTypeToString:=s;
end;
end.